package com.alibaba.citrus.cr.price.ability.functions.factory.price.process;

import com.alibaba.citrus.cr.common.annotation.RepositoryInvoker;
import com.epoch.app.crpricecenter.enums.DimensionPropertyEnum;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author hanguo
 * @date 2021/11/19
 * @Description 价格列表中属性字段描述的管理类
 */
@Component
public class PropertyProcessManager implements ApplicationContextAware {

    private static Map<String, PriceProcess> map = new ConcurrentHashMap<>();

    /**
     * bean的装配
     * @param applicationContext
     * @throws BeansException
     */
    @Override
    @RepositoryInvoker
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        String[] beanNamesForType = applicationContext.getBeanNamesForType(PriceProcess.class);
        for (String beanName : beanNamesForType) {
            PriceProcess bean = applicationContext.getBean(beanName, PriceProcess.class);
            map.put(bean.getPropertyCode(), bean);
            //销售组织、销售组、销售部门都使用一个处理器
            if (bean.getPropertyCode().equals(DimensionPropertyEnum.ORG_SALES_ORGANIZATION_CODE.getKey())) {
                map.put(DimensionPropertyEnum.SALES_GROUP.getKey(), bean);
                map.put(DimensionPropertyEnum.SALES_DEPARTMENT.getKey(), bean);
            }
        }
    }

    /**
     * 获取属性处理器
     *
     * @param propertyCode
     * @return
     */
    public PriceProcess getPropertyProcess(String propertyCode) {
        return map.get(propertyCode);
    }
}
